package com.magictower.primary.activity;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.net.TrafficStats;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.format.Formatter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.magictower.primary.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author kejunzhan
 * @since 2017/12/25
 */

public class TrafficManagerActivity extends AppCompatActivity {

    private ListView lv_view;
    private List<TrafficModel> list;
    private MyTrafficAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_traffic_manager);

        lv_view = (ListView) findViewById(R.id.lv_view);
        initData();
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            adapter = new MyTrafficAdapter();
            lv_view.setAdapter(adapter);
        }
    };

    private void initData() {
        new Thread(){
            @Override
            public void run() {
                list = getTrafficData();
                handler.sendEmptyMessageDelayed(0, 1000);
            }
        }.start();
    }

    /**
     * 获取各个应用流量统计
     * @return
     */
    private List<TrafficModel> getTrafficData() {
        list = new ArrayList<>();
        TrafficModel model;
        PackageManager pm = getPackageManager();
        List<PackageInfo> packageInfos = pm.getInstalledPackages(0);
        for (PackageInfo info : packageInfos) {
            model = new TrafficModel();
            int uid = info.applicationInfo.uid;
            long uidRxBytes = TrafficStats.getUidRxBytes(uid);//接收数据
            long uidTxBytes = TrafficStats.getUidTxBytes(uid);//上传数据
            if (uidRxBytes > 0 && uidTxBytes > 0) {
                String appName = info.applicationInfo.loadLabel(pm).toString();//应用名
                Drawable icon = info.applicationInfo.loadIcon(pm);
                model.appName = appName;
                model.icon = icon;
                model.receiveCount = uidRxBytes;
                model.sendCount = uidTxBytes;
                list.add(model);
            }
        }
        return list;
    }

    private static class TrafficModel {
        String appName;
        Drawable icon;
        long sendCount;
        long receiveCount;
    }

    private class MyTrafficAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(TrafficManagerActivity.this, R.layout.item_traffic_manager, null);
                holder = new ViewHolder();
                holder.iv_icon = (ImageView) convertView.findViewById(R.id.iv_icon);
                holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
                holder.tv_desc = (TextView) convertView.findViewById(R.id.tv_desc);
                holder.tv_total = (TextView) convertView.findViewById(R.id.tv_total);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            TrafficModel bean = list.get(position);
            holder.iv_icon.setImageDrawable(bean.icon);
            holder.tv_name.setText(bean.appName);
            String desc = "上传：" + Formatter.formatFileSize(TrafficManagerActivity.this, bean.sendCount) +
                    "\t\t下载：" + Formatter.formatFileSize(TrafficManagerActivity.this, bean.receiveCount);
            holder.tv_desc.setText(desc);
            String total = "共" + Formatter.formatFileSize(TrafficManagerActivity.this, bean.sendCount + bean.receiveCount);
            holder.tv_total.setText(total);

            return convertView;
        }
    }

    class ViewHolder {
        ImageView iv_icon;
        TextView tv_name;
        TextView tv_desc;
        TextView tv_total;
    }
}
